package com.kaiyidiannao.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.kaiyidiannao.activity.user_detail.MyPublish;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.MessageBean;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.model.GetCloseOpen;
import com.kaiyidiannao.model.req.model.PostDeleteMessageInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wzg on 14-4-14.
 */
public class MessageAdapter extends ImageLoaderBaseAdapter{

    private ListView listView;
    private Context context;
    private int count= 0;
    private LayoutInflater layoutInflater;
    private List<MessageBean> list = new ArrayList<MessageBean>();
    private String userid ;
    private ArrayList<MessageBean> messageList = DataCenter.messageInfoList;
    private int typeid = DataCenter.typeid;
    private ImageLoader imageLoader;
    private int tip = 0;
    private ArrayList<UserBean> userBeans = DataCenter.userLogin;

    public MessageAdapter(Context context, List<MessageBean> list) {
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    public MessageAdapter(Context context, List<MessageBean> list,int tip) {
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.tip = tip;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView info_date = null;
        TextView info_time = null;
        TextView hot = null;
        ImageView closeOrOpen = null;
        TextView info_title = null;
        ImageButton delete = null;
        ImageView imageView = null;
        MessageBean messageBean = messageList.get(position);
        TextView hotText = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.message_item, null);
        }
        closeOrOpen = (ImageView) convertView.findViewById(R.id.closeOrOpen);
//        imageView = (ImageView)convertView.findViewById(R.id.imageView);
        info_date = (TextView) convertView.findViewById(R.id.info_date);
        info_time = (TextView) convertView.findViewById(R.id.info_time);
        info_title = (TextView) convertView.findViewById(R.id.info_title);
        hotText = (TextView) convertView.findViewById(R.id.hotText);
        hot = (TextView) convertView.findViewById(R.id.hot);
        if(tip==1){
            closeOrOpen.setVisibility(View.VISIBLE);
        }

        if(list.get(position).getStatus()!=null){
            if (list.get(position).getStatus().equals("2")) {
                closeOrOpen.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_dark_button));
            }else {
                closeOrOpen.setImageBitmap(BitmapFactory.decodeResource(convertView.getResources(), R.drawable.mypublish_light_button));
            }
        }

        closeOrOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DataCenter.position = position;
                ((BaseActionBarActivity)context).showProgressDialog();
                if (Integer.parseInt(list.get(position).getStatus()) == 2) {
                    new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "open", DataCenter.messageInfoList.get(position).getItemid(), "30", 56 + "").execute();
                } else if (Integer.parseInt(list.get(position).getStatus()) == 3) {
                    new GetCloseOpen(DataCenter.userLogin.get(0).getUserid(), "close", DataCenter.messageInfoList.get(position).getItemid(), "30", 56 + "").execute();
                }
                MyPublish publish = (MyPublish) context;
                publish.showProgressDialog();
                Log.d("CloseOrOpen",position+"   #");
            }
        });
        delete = (ImageButton) convertView.findViewById(R.id.btDelete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(typeid==56){
                    MessageBean bean = messageList.get(position);
                    String action;
                    action = "delete";
                    DataCenter.position = position;
                    if(DataCenter.userLogin.size()!=0){
                        userid = DataCenter.userLogin.get(0).getUserid();
                        ((BaseActionBarActivity)context).showProgressDialog();
                        new PostDeleteMessageInfo(bean.getFid(),userid,action).execute();
                    }
                    Log.d("MyFavoritesActivity", bean.getFid()+"  " + position+"");
                }
            }
        });
//
//        if (DataCenter.tip == 1) {
//            for (int i = 0; i < getCount(); i++) {
//
//                hot.setVisibility(View.GONE);
//                hotText.setVisibility(View.GONE);
//                delete.setVisibility(View.VISIBLE);
//                DataCenter.tip = 0;
//            }
//        }
//        info_date.setText(messageBean.getAddtime());
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String date = sdf.format(new Date((Long.parseLong(messageBean.getAddtime()))*1000));
        String date = messageBean.getAddtime()+"";
        info_time.setText(date);
//        imageLoader.displayImage(messageBean.getThumb(),imageView,options,null);
        hot.setText("  "+messageBean.getHits());
        info_title.setText(messageBean.getTitle());
        return convertView;
    }

}
